package com.ganbo.stock.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Map;

/**
 * Map集合工具类
 *
 * @author brucegan
 * @date 2019-08-14 11:30
 */
public class MapUtils {


    /**
     * Map转对象
     *
     * @param type
     * @param map
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> T transObj(Class<T> type, Map map) throws Exception {
        T obj = type.newInstance(); //实例化类
        BeanInfo info = Introspector.getBeanInfo(type); //获取类中属性
        PropertyDescriptor[] propertyPermissions = info.getPropertyDescriptors();
        for (PropertyDescriptor pro : propertyPermissions) {
            String proName = pro.getName();
            if (map.containsKey(proName)) {
                Object methodName = map.get(proName);
                Object[] args = {methodName};
                pro.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }




}
